﻿using LCL.Caching;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace LCL.DbObjects.SQLite
{
    [Serializable]
    [DebuggerDisplay("{ConnectionString}")]
    public class DbObject : DbModelBase, IDbObject
    {
        public List<DataBaseModel> GetDBList(string host)
        {
            if (string.IsNullOrWhiteSpace(host)) return new List<DataBaseModel>();
            string cacheKey = string.Format(CacheKey_DBLIST, host);
            return null;
        }

        public List<TableModel> GetTables(string dbName, bool iscol = false)
        {
            if (string.IsNullOrWhiteSpace(dbName)) return new List<TableModel>();
            string cacheKey = string.Format(CacheKey_TABLELIST, dbName);
            return cache.Get(cacheKey, () =>
            {
                var list = new List<TableModel>();
                var dt = DbHelper.ExecuteDataTable("select name from sqlite_master where type='table' order by name", null);
                int count = dt.Rows.Count;
                foreach (DataRow row in dt.Rows)
                {
                    string tableName = row["name"].ToString();
                    if (!this.ArrNoTableslist.Contains(tableName))
                    {
                        string comments = string.Empty;
                        var tm = new TableModel();
                        tm.TableName = tableName;
                        tm.TableNameRemark = comments;
                        list.Add(tm);
                    }
                }
                return list;
            });
        }

        public TableModel GetTableInfo(string dbName, string tableName)
        {
            if (string.IsNullOrWhiteSpace(dbName) || string.IsNullOrWhiteSpace(tableName)) return null;
            string cacheKey = string.Format(CacheKey_TABLE_INFO, dbName, tableName);
            return cache.Get(cacheKey, () =>
            {
                var tables = this.GetTables(dbName);
                TableModel tm = tables.FirstOrDefault(p => p.TableName == tableName);
                if (tm.Columns == null || tm.Columns.Count == 0)
                {
                    tm.Columns = GetColumnInfoList(dbName, tableName);
                }
                return tm;
            });
        }

        public List<TableColumn> GetColumnInfoList(string dbName, string tableName)
        {
            if (string.IsNullOrWhiteSpace(dbName) || string.IsNullOrWhiteSpace(tableName)) return null;
            string cacheKey = string.Format(CacheKey_TABLE_COL, dbName, tableName);
            return cache.Get(cacheKey, () =>
            {
                string strSql = "pragma table_info('" + tableName + "');";
                var list = new List<TableColumn>();
                var dt = DbHelper.ExecuteDataTable(strSql, null);
                int order = 1;
                foreach (DataRow row in dt.Rows)
                {
                    var tc = new TableColumn();
                    tc.ColumnOrder = Utils.GetObjTranNull<int>(row["cid"].ToString());
                    tc.TableName = tableName;
                    tc.ColumnName = row["name"].ToString();
                    tc.ColumnRemark = "";
                    tc.ColumnType = row["type"].ToString();
                    tc.DefaultValue = row["dflt_value"].ToString();
                    tc.MaxLength = 0;
                    tc.IsPrimaryKey = Utils.GetObjTranNull<int>(row["pk"].ToString())==1 ? true : false;
                    tc.IsIdentity = false;
                    tc.IsNullable = Utils.GetObjTranNull<int>(row["notnull"].ToString()) == 1 ? true : false;
                    string typename = tc.ColumnType, len = "", pre = "", scal = "";
                    TypeNameProcess(tc.ColumnType, out typename, out len, out pre, out scal);
                    tc.ColumnType = typename;
                    tc.MaxLength =  Utils.GetObjTranNull<int>(len);

                    list.Add(tc);
                    order++;
                }
                return list;
            });
        }

        public List<TableRelation> GetTableRelation(string tableName)
        {
            return new List<TableRelation>();
        }

        public void AddTableByKey(string dbName)
        {
            
        }
        /// <summary>
        /// 添加表（字段）描述
        /// </summary>
        /// <param name="addbyupdate"></param>
        /// <param name="describe"></param>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        public void ExtendedProperty(bool addbyupdate, string describe, string tableName, string columnName)
        {
           
        }

       
    }
}
